package com.zhi.controller;

import com.zhi.config.Constant;
import com.zhi.entity.Faculty;
import com.zhi.entity.Teacher;
import com.zhi.service.FacultyService;
import com.zhi.service.MaxValueService;
import com.zhi.util.PageUtil;
import com.zhi.util.PubFun;
import com.zhi.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 学院信息Controller
 */
@Controller
public class FacultyController {

    Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
    FacultyService facultyService;

	@Autowired
	MaxValueService maxValueService;

    /**
     * 学院列表
     * @param model
     * @param pageUtil
     * @return
     */
    @RequestMapping("/toFaculty")
    public String toFaculty(Model model,PageUtil pageUtil) {
    	
    	//1、初始化分页工具
    	pageUtil = PageUtil.init(pageUtil);
    	Pageable pageable = PageRequest.of(pageUtil.getPageIndex()-1, pageUtil.getSize());
    	
    	//2、查询分页数据
    	Page<Faculty> pageList=facultyService.getEntityListWithCodeName(pageable);
    	List<Faculty> facultys = pageList.getContent();
        
        //3、设置总页数
    	pageUtil.setTotalPages(pageList.getTotalPages());
    	
    	//4、返回前台数据
    	model.addAttribute("pageUtil", pageUtil);
        model.addAttribute("facultys", facultys);
        return "faculty/list";
    }

    /**
     * 跳转增加学院列表
     * @param model
     * @return
     */
    @RequestMapping("/faculty/toAdd")
    public String toAdd(Model model) {
        return "faculty/add";
    }

    /**
     * 增加学院
     * @param faculty
     * @return
     */
    @RequestMapping("/faculty/add")
    @ResponseBody
    public R add(Faculty faculty) {

        //学院编号：学院编辑标识号+序号，例3001 001
        //查询code对应的最大值，若无结果取0并更新1，若有结果取n并更新n+1
        Integer maxvalue = maxValueService.getMax(Constant.ID_BEGIN_FACULTY);
        if(maxvalue == null){
            maxvalue = 0;
        }
        maxValueService.saveCodeAndValue(Constant.ID_BEGIN_FACULTY,maxvalue + 1);
        //取得最大值后，补全位数要求
        String facultyIdEnd = PubFun.getStandardChar(maxvalue + 1,3);
        //补全编号前缀
        String facultyId = Constant.ID_BEGIN_FACULTY + facultyIdEnd;
        faculty.setFacultyId(facultyId);

        facultyService.save(faculty);
        return R.ok();
    }

    /**
     * 跳转编辑学院列表
     * @param model
     * @param facultyId
     * @return
     */
    @RequestMapping("/faculty/toEdit")
    public String toEdit(Model model,String facultyId) {
        Faculty faculty=facultyService.getOne(facultyId);
        model.addAttribute("faculty", faculty);
        return "faculty/edit";
    }

    /**
     * 编辑学院
     * @param faculty
     * @return
     */
    @RequestMapping("/faculty/edit")
    public String update(Faculty faculty) {
        facultyService.edit(faculty);
        return "redirect:/toFaculty";
    }

    /**
     * 删除学院
     * @param facultyId
     * @return
     */
    @RequestMapping("/faculty/delete")
    public String delete(String facultyId) {
        facultyService.delete(facultyId);
        return "redirect:/toFaculty";
    }


}
